package com.sneakxy.mybase4j.base.fastdfs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

public class FastDfsClient implements InitializingBean, IFastDfsClient {
	
	private Resource configPath;

	private TrackerClient trackerClient;
	
	public void setConfigPath(Resource configPath) {
		this.configPath = configPath;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		if(configPath != null) {
			ClientGlobal.init(this.configPath.getFile().getAbsolutePath());
		} else {
			ClientGlobal.init(this.getClass().getClassLoader().getResource("fdfs_client.conf").getFile());
		}
		trackerClient = new TrackerClient();
	}
	
	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.fastdfs.IFastDfsClient#deleteFile(java.lang.String)
	 */
	@Override
	public int deleteFile(String file_id) throws IOException, MyException {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient2 storageClient2 = null;
		try {
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient2 = new StorageClient2(trackerServer, storageServer);
			return storageClient2.delete_file1(file_id);
		} catch (IOException e) {
			throw e;
		} catch (MyException e) {
			throw e;
		} finally {
			if(storageServer != null) {
				storageServer.close();
			}
			if(trackerServer != null) {
				trackerServer.close();
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.fastdfs.IFastDfsClient#downloadFile(java.lang.String, java.io.OutputStream)
	 */
	@Override
	public int downloadFile(String file_id, OutputStream out) throws IOException, MyException {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient2 storageClient2 = null;
		try {
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient2 = new StorageClient2(trackerServer, storageServer);
			return storageClient2.download_file2(file_id, out);
		} catch (IOException e) {
			throw e;
		} catch (MyException e) {
			throw e;
		} finally {
			if(storageServer != null) {
				storageServer.close();
			}
			if(trackerServer != null) {
				trackerServer.close();
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.fastdfs.IFastDfsClient#uploadFile(java.lang.String, java.io.InputStream, long, java.lang.String)
	 */
	@Override
	public String uploadFile(String group_name, InputStream is, long file_size, String file_ext_name) throws IOException, MyException {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient2 storageClient2 = null;
		try {
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient2 = new StorageClient2(trackerServer, storageServer);
			return storageClient2.upload_file2(group_name, is, file_size, file_ext_name, null);
		} catch (IOException e) {
			throw e;
		} catch (MyException e) {
			throw e;
		} finally {
			if(storageServer != null) {
				storageServer.close();
			}
			if(trackerServer != null) {
				trackerServer.close();
			}
		}
	}
	
	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.base.fastdfs.IFastDfsClient#uploadFile(java.io.InputStream, long, java.lang.String)
	 */
	@Override
	public String uploadFile(InputStream is, long file_size, String file_ext_name) throws IOException, MyException {
		TrackerServer trackerServer = null;
		StorageServer storageServer = null;
		StorageClient2 storageClient2 = null;
		try {
			trackerServer = trackerClient.getConnection();
			storageServer = trackerClient.getStoreStorage(trackerServer);
			storageClient2 = new StorageClient2(trackerServer, storageServer);
			return storageClient2.upload_file2(is, file_size, file_ext_name, null);
		} catch (IOException e) {
			throw e;
		} catch (MyException e) {
			throw e;
		} finally {
			if(storageServer != null) {
				storageServer.close();
			}
			if(trackerServer != null) {
				trackerServer.close();
			}
		}
	}
	
}
